//加载供应商列表
function loadData_provider() {
    $.ajax({
        url: "../provider/findAll",
        dataType: "json",
        async:false,
        success:function (result) {
            if (result.flag){
                //渲染数据
                renderData_provider(result.data);
            }else{
                alert(result.msg);
            }
        },
        error: function (jqxhr) {
            alert("请求出错：" + jqxhr.status);
        }
    });
}

//渲染供应商列表
function renderData_provider(list){
    var template = $("#providerId option:first");

    //遍历数据
    for (var i in list) {
        var option = template.clone();
        option.text(list[i].proName);
        option.attr("value", list[i].id);

        $("#providerId").append(option);
    }
}


//校验表单
var flag;
function checkForm(type){
    checkBillCode(type);
    if(!flag){
        $("#billCode")[0].setCustomValidity("编码已经被占用!");
    }else {
        $("#billCode")[0].setCustomValidity("");
    }

    var providerId = $("#providerId")[0];
    if(providerId.value == 0){
        if($("#providerId").value == 0){
            $("#providerId").setCustomValidity("请选择供应商");
        }else {
            $("#providerId").setCustomValidity("");
        }
    }
}

//校验订单编码
function checkBillCode(type){
    var url;
    if(type == "add"){
        url = "../bill/checkByBillCode"
    }else if(type == "update"){
        url = "../bill/checkByBillCodeUpd"
    }
    $.ajax({
        url:url,
        data:{"billCode":$("#billCode").val(), "self":self},
        dataType:"json",
        async:false,
        success:function (result){
            flag = result.flag;
        },
        error:function (jqxhr){
            //注
            alert("请求出错：" + jqxhr.status);
        }
    });
}

//上传文件
function upload(){
    var file = $("#file")[0].files[0];

    //FormDate对象
    var formData = new FormData();
    //添加数据， key-value
    formData.append("file", file);

    $.ajax({
        url:"../bill/fileUpload",
        type: "post",
        dataType: "json",
        data: formData,
        processData: false, //默认为true,请求参数会转化成一个查询字符串,false则不处理
        contentType: false, //指定为false才能形成正确的Content-Type="multipart/form-data"
        success:function (result) {
            if (result.flag) {
                //将保存后的文件名保存在该标签中，方便保存到数据库中
                $("#img").val(result.data);
                var fileName = result.data;
                var index = fileName.lastIndexOf(".");
                var ext = fileName.substr(index+1);
                if(ext == "jpg"){
                    $("img").attr("src", "../upload/" + result.data);
                }else {
                    $("img").attr("src","");
                }
            } else {
                alert(result.msg);
            }
        }
    })
}